<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px" @submit.native.prevent>
      <el-form-item label="考核主题" prop="cycleTitle">
        <el-input v-model="queryParams.cycleTitle" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cld:checkCycle:add']">新增考核周期</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkCycleList" border>
      <el-table-column label="考核主题" align="center" prop="cycleTitle" />
      <el-table-column label="考核年份" align="center" prop="cycleYear" />
      <el-table-column label="考核开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考核结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考核说明" align="center" prop="remark" />
      <el-table-column label="提交人" align="center" prop="createName" />
      <el-table-column label="提交时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:checkCycle:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cld:checkCycle:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改考核周期对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="top" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考核主题" prop="cycleTitle">
              <el-input v-model="form.cycleTitle" placeholder="请输入考核周期标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核年份" prop="cycleYear">
              <el-date-picker v-model="form.cycleYear" format="yyyy" value-format="yyyy" type="year" placeholder="选择年">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考核开始时间" prop="startTime">
              <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择考核开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核结束时间" prop="endTime">
              <el-date-picker clearable v-model="form.endTime" value-format="yyyy-MM-dd" type="date"
                placeholder="请选择考核结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="考核说明" prop="remark">
              <el-input type="textarea" :rows="4" v-model="form.remark" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCheckCycle,
  getCheckCycle,
  delCheckCycle,
  addCheckCycle,
  updateCheckCycle,
} from "@/api/cld/checkCycle";

export default {
  name: "CheckCycle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考核周期表格数据
      checkCycleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cycleTitle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cycleTitle: [
          { required: true, message: '请输入考核主题', trigger: 'blur' }
        ],
        cycleYear: [
          { required: true, message: '请选择考核年份', trigger: 'change' }
        ],
        startTime: [
          { required: true, message: '请选择考核开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择考核结束时间', trigger: 'change' }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询考核周期列表 */
    getList() {
      this.loading = true;
      listCheckCycle(this.queryParams).then((response) => {
        this.checkCycleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cycleId: null,
        cycleTitle: null,
        startTime: null,
        endTime: null,
        remark: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考核周期";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cycleId = row.cycleId || this.ids;
      getCheckCycle(cycleId).then((response) => {
        this.form = response.data;
        this.form.cycleYear = String(this.form.cycleYear)
        this.open = true;
        this.title = "修改考核周期";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form)
          if (this.form.cycleId != null) {
            updateCheckCycle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckCycle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cycleIds = row.cycleId || this.ids;
      this.$modal
        .confirm('是否确认删除考核周期编号为"' + cycleIds + '"的数据项？')
        .then(function () {
          return delCheckCycle(cycleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cld/checkCycle/export",
        {
          ...this.queryParams,
        },
        `checkCycle_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
